//
//  WTHomePageViewController.m
//  Tourism
//
//  Created by WZH on 2018/7/3.
//  Copyright © 2018年 WZH. All rights reserved.
//

#import "WTHomePageViewController.h"
#import "WTOutdoorKnowledgeModel.h"
#import "WTForeignModel.h"
@interface WTHomePageViewController ()

@end

@implementation WTHomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self Http];
    
}

- (void)Http{
    
    [HJTNetTool get1:ForeignURL progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(id  _Nonnull responseObject) {
        [self dealWithString:responseObject];
    } failure:^(NSString * _Nonnull errorLD) {
        
    }];
    
}


#pragma mark 逻辑性处理字符串
- (void)dealWithString:(NSString *)html{
//    <!--帖子开始-->
//    <!--帖子结束-->
    ForeignURL;
    html = [html componentsSeparatedByString:@"<!--帖子开始-->"].lastObject;
    html = [html componentsSeparatedByString:@"<!--帖子结束-->"].firstObject;
    html = [html componentsSeparatedByString:@"article\">"].lastObject;
    NSMutableArray <NSString *>*array = [html componentsSeparatedByString:@"人浏览"].mutableCopy;
    [array removeLastObject];
    for (int i = 0; i < array.count; i ++) {
        WTForeignModel *model = [[WTForeignModel alloc] init];
        NSString *logicString = array[i];
        if ([array[i] rangeOfString:@"aotitle"].location != NSNotFound) {
            model.title     = [[logicString componentsSeparatedByString:@"class=\"aotitle\">"].lastObject componentsSeparatedByString:@"</a>"].firstObject;
            model.image1    = [[logicString componentsSeparatedByString:@"<img src='"][1] stringByReplacingOccurrencesOfString:@"'/>" withString:@""];
            model.image2    = [[logicString componentsSeparatedByString:@"<img src='"][2] stringByReplacingOccurrencesOfString:@"'/>" withString:@""];
            model.image3    = [[logicString componentsSeparatedByString:@"<img src='"][3] stringByReplacingOccurrencesOfString:@"'/>" withString:@""];
            model.countries = [[logicString componentsSeparatedByString:@"class=\"alabel\">"].lastObject componentsSeparatedByString:@"</a>"].firstObject;
            model.author    = [[logicString componentsSeparatedByString:@"class=\"aname\">"].lastObject componentsSeparatedByString:@"</a>"].firstObject;
            model.portrait  = [[logicString componentsSeparatedByString:@"id=\"touxiangimg\" src=\""].lastObject componentsSeparatedByString:@";\""].firstObject;
            model.url       = [[logicString componentsSeparatedByString:@"' target=\"_blank\""].firstObject componentsSeparatedByString:@"<a href='"].lastObject;
            model.number    = [logicString componentsSeparatedByString:@"<p class=\"anum\">"].lastObject;
            model.type      = ThreeImageType;
        }else if ([array[i] rangeOfString:@"azl"].location != NSNotFound) {
            model.title     = @"";
            model.image1    = @"";
            model.image2    = @"";
            model.image3    = @"";
            model.countries = @"";
            model.author    = @"";
            model.portrait  = @"";
            model.url       = @"";
            model.number    = @"";
            model.type      = OneImageType;
        }else if ([array[i] rangeOfString:@"atitle"].location != NSNotFound) {
            model.title     = @"";
            model.image1    = @"";
            model.image2    = @"";
            model.image3    = @"";
            model.countries = @"";
            model.author    = @"";
            model.portrait  = @"";
            model.url       = @"";
            model.number    = @"";
            model.type      = NoneImageType;
        }else{
            NSLog(@"未知model");
        }
    }
//    [array removeFirstObject];
//    array[array.count - 1] = [array[array.count - 1] componentsSeparatedByString:@"浏览"].firstObject;
}

#pragma mark 户外知识处理方式
- (void)dealKnowledge:(NSString *)html{
    //    <!--首页头部广告-->     <!--左侧开始-->
    //    <!--首页底部广告-->     <!--左侧结束-->
    //
    KnowledgeURL;
    html = [html componentsSeparatedByString:@"<!--左侧开始-->"].lastObject;
    html = [html componentsSeparatedByString:@"<!--左侧结束-->"].firstObject;
    NSMutableArray <NSString *>*array = [html componentsSeparatedByString:@"<li>"].mutableCopy;
    [array removeFirstObject];
    for (int i = 0; i < array.count; i ++) {
        array[i] = [array[i] componentsSeparatedByString:@"</li>"].firstObject;
        WTOutdoorKnowledgeModel *model = [[WTOutdoorKnowledgeModel alloc] init];
        model.title     = [[array[i] componentsSeparatedByString:@"target='_blank'>"].lastObject componentsSeparatedByString:@"</a></h3>"].firstObject;
        model.time      = [[array[i] componentsSeparatedByString:@"时间："].lastObject componentsSeparatedByString:@"</em>"].firstObject;
        model.content   = [[array[i] componentsSeparatedByString:@"<span>"].lastObject componentsSeparatedByString:@"</span>"].firstObject;
        model.author    = [[array[i] componentsSeparatedByString:@"作者："].lastObject componentsSeparatedByString:@"</em>"].firstObject;
        model.url       = [[array[i] componentsSeparatedByString:@"href=\""].lastObject componentsSeparatedByString:@"\""].firstObject;
        model.imageUrl  = [[array[i] componentsSeparatedByString:@"img src=\""].lastObject componentsSeparatedByString:@"\">"].firstObject;
        model.author    = [[model.author componentsSeparatedByString:@"来源于"].firstObject stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
        NSLog(@"%@",model.modelToJSONString);
    }
}



@end
